<!doctype html>
<html lang="en">

<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="Awesome TiddlyWiki Editions" name="application-name">
    <meta content="v5.2.2" name="application-version">
    <meta content="Copyright 2015 Jeremy Ruston" name="copyright">
    <link href="favicon.ico" id="faviconLink" rel="shortcut icon">
    <title>Awesome TiddlyWiki Editions</title>
    <script>
        var assetList = '%%plugins%%';
        ! function() {
            "use strict";

            // Listen for window messages
            window.addEventListener("message", function listener(event) {
                console.log("plugin library: Received message from", event.origin);
                console.log("plugin library: Message content", event.data);
                switch (event.data.verb) {
                    case "GET":
                        if (event.data.url === "recipes/library/tiddlers.json") {
                            // Route for recipes/library/tiddlers.json
                            event.source.postMessage({
                                verb: "GET-RESPONSE",
                                status: "200",
                                cookies: event.data.cookies,
                                url: event.data.url,
                                type: "application/json",
                                body: JSON.stringify(assetList, null, 4)
                            }, "*");
                        } else if (event.data.url.indexOf("recipes/library/tiddlers/") === 0) {
                            var url = "plugins/" + encodeURIComponent(
                                decodeURIComponent(removePrefix(event.data.url, "recipes/library/tiddlers/"))
                                .replace('$:/plugins/', '')
                                .replace('$:/languages/', 'languages_')
                                .replace('$:/themes/', 'themes_')
                                .replace(/[:/<>"\|?*]/g, '_')
                            );
                            // Route for recipes/library/tiddlers/<uri-encoded-tiddler-title>.json
                            httpGet(url, function(err, responseText) {
                                if (err) {
                                    event.source.postMessage({
                                        verb: "GET-RESPONSE",
                                        status: "404",
                                        cookies: event.data.cookies,
                                        url: event.data.url,
                                        type: "text/plain",
                                        body: "Not found"
                                    }, "*");
                                } else {
                                    event.source.postMessage({
                                        verb: "GET-RESPONSE",
                                        status: "200",
                                        cookies: event.data.cookies,
                                        url: event.data.url,
                                        type: "application/json",
                                        body: responseText
                                    }, "*");
                                }
                            });
                        } else {
                            event.source.postMessage({
                                verb: "GET-RESPONSE",
                                status: "404",
                                cookies: event.data.cookies,
                                url: event.data.url,
                                type: "text/plain",
                                body: "Not found"
                            }, "*");
                        }
                        break;
                }
            }, false);

            // Helper to remove string prefixes
            function removePrefix(string, prefix) {
                if (string.indexOf(prefix) === 0) {
                    return string.substr(prefix.length);
                } else {
                    return string;
                }
            }

            // Helper for HTTP GET
            function httpGet(url, callback) {
                var http = new XMLHttpRequest();
                http.open("GET", url, true);
                http.onreadystatechange = function() {
                    if (http.readyState == 4 && http.status == 200) {
                        callback(null, http.responseText);
                    }
                };
                http.send();
            }

        }()
    </script>
</head>

<body>
    <h1>HelloThere</h1>
    <p>This is the TiddlyWiki plugin library. It is not intended to be opened directly in the browser.
        See <a href="https://tiddlywiki.com/" target="_blank">https://tiddlywiki.com/</a>
        for details of how to install plugins.</p>
</body>

</html>
